package com.haliyoo.adhere.http;

import java.io.IOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import weibo4android.org.json.JSONArray;
import weibo4android.org.json.JSONException;
import weibo4android.org.json.JSONObject;
import android.util.Log;

import com.haliyoo.adhere.bean.WeiboBean;
import com.haliyoo.adhere.controller.Constants;

public abstract class AdClient {
	private static final String TAG = "AdClient";
	
	/**
	 * 导出登录响应
	 * @param code 错误码
	 * @param userId 用户名
	 * @param sign 签名
	 */
	public abstract void exportLoginResponse(long code, String userId, String sign);
	
	/**
	 * 导出退出响应
	 * @param code 错误码
	 */
	public abstract void exportLogoutResponse(long code);
	
	/**
	 * 导出处转发响应
	 * @param code 错误码
	 */
	public abstract void exportRepostResponse(long code);
	
	/**
	 * 导出发布响应
	 * @param code 错误码
	 */
	public abstract void exportPostResponse(long code);
	
	/**
	 * 导出获取广告列表响应
	 * @param code 错误码
	 * @param wbList 广告列表
	 */
	public abstract void exportListResponse(long code, List<WeiboBean> wbList);

	/**
	 * 导出获取创意/魅力/贡献/童鞋数目响应
	 * @param code 错误码
	 * @param customerId 带查用户ID
	 * @param creativeCount 创意数目
	 * @param charmCount 魅力数目
	 * @param contributionsCount 贡献数目
	 * @param colleaguesCount 童鞋数目
	 */
	public abstract void exportCountResponse(long code, String customerId, int creativeCount,
			int charmCount, int contributionsCount, int colleaguesCount);

	/**
	 * 登录
	 * @param user 用户id/用户名
	 * @param pwd 密码
	 * @return 请求返回状态
	 */
	public int login(String user, String pwd) {
		long code = 0;
		String sign = null;
		String userId = null;

		/// 登录
    	try {
    		HttpData recvData = HttpClient.loginRequest(Constants.SERVER_ADDR, 
    				user, pwd);
    		
    		if (recvData == null) {
    			Log.e(TAG, "recv data null.");
    			
    			return -1;
    		} else {
    			
    			/// 没有获取到数据
    			if (recvData.getByteArray() == null) {
    				return -1;
    			}
    			
    			/// 解析数据
    			String ResponseString = new String(recvData.getByteArray());
    			JSONObject object = null;
    			/// 解析错误码
				try {
					String strCode = null;
					
					object = new JSONObject(ResponseString);
					strCode = object.getString("code");
					code = Integer.parseInt(strCode);
					//code = object.getLong("code");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    			
    			/// 状态码正确
    			if (code == 1) {
    				/// 解析签名
    				try {
    					JSONArray list = object.getJSONArray("dataList");
    					
    					sign = list.getString(0);
    					userId = list.getString(1);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
    			} else {
    				Log.e(TAG, "code = " + code);
    			}
    		}
		} catch (SocketTimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/// 导出登录响应数据
		exportLoginResponse(code, userId, sign);
		
		return (int) code;
	}
	
	/**
	 * 退出
	 * @param user 用户id/用户名
	 * @param sign 签名
	 * @return 请求返回状态
	 */
	public int logout(String user, String sign) {
		long code = 0;

		/// 登录
    	try {
    		HttpData recvData = HttpClient.logoutRequest(Constants.SERVER_ADDR, 
    				user, sign);
    		
    		if (recvData == null) {
    			Log.e(TAG, "recv data null.");
    			
    			return -1;
    		} else {
    			
    			/// 没有获取到数据
    			if (recvData.getByteArray() == null) {
    				return -1;
    			}
    			
    			/// 解析数据
    			String ResponseString = new String(recvData.getByteArray());
    			JSONObject object = null;
    			/// 解析错误码
				try {
					String strCode = null;
					
					object = new JSONObject(ResponseString);
					strCode = object.getString("code");
					code = Integer.parseInt(strCode);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    		}
		} catch (SocketTimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/// 导出退出响应数据
		exportLogoutResponse(code);
		
		return (int) code;
	}
	
	/**
	 * 转发
	 * @param user 用户id/用户名
	 * @param adIds 广告id集合
	 * @param sign 签名
	 * @return 请求返回状态
	 */
	public int repost(String user, String[] adIds, String sign) {
		long code = 0;

		/// 登录
    	try {
    		HttpData recvData = HttpClient.repostRequest(Constants.SERVER_ADDR, 
    				user, adIds, sign);
    		
    		if (recvData == null) {
    			Log.e(TAG, "recv data null.");
    			
    			return -1;
    		} else {
    			
    			/// 没有获取到数据
    			if (recvData.getByteArray() == null) {
    				return -1;
    			}

    			/// 解析数据
    			String ResponseString = new String(recvData.getByteArray());
    			JSONObject object = null;
    			/// 解析错误码
				try {
					String strCode = null;
					
					object = new JSONObject(ResponseString);
					strCode = object.getString("code");
					code = Integer.parseInt(strCode);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    		}
		} catch (SocketTimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/// 导出转发响应数据
		exportRepostResponse(code);
		
		return 0;
	}
	
	/**
	 * 发布
	 * @param user 用户id/用户名
	 * @param adId 广告id
	 * @param sign 签名
	 * @return 请求返回状态
	 */
	public int post(String user, String adId, String sign) {
		long code = 0;

		/// 登录
    	try {
    		HttpData recvData = HttpClient.postRequest(Constants.SERVER_ADDR, 
    				user, adId, sign);
    		
    		if (recvData == null) {
    			Log.e(TAG, "recv data null.");
    			
    			return -1;
    		} else {
    			
    			/// 没有获取到数据
    			if (recvData.getByteArray() == null) {
    				return -1;
    			}

    			/// 解析数据
    			String ResponseString = new String(recvData.getByteArray());
    			JSONObject object = null;
    			/// 解析错误码
				try {
					String strCode = null;
					
					object = new JSONObject(ResponseString);
					strCode = object.getString("code");
					code = Integer.parseInt(strCode);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    		}
		} catch (SocketTimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/// 导出发布响应数据
		exportPostResponse(code);
		
		return 0;
	}
	
	/**
	 * 获取广告列表
	 * @param user 用户id/用户名
	 * @param sign 签名
	 * @return 请求返回状态
	 */
	public int list(String user, String sign) {
		//"id":2,"weiboId":2,"userId":39,"createTime":"2011-08-16 19:12:10"
		List<WeiboBean> wbList = new ArrayList<WeiboBean>();
		long code = 0;

		/// 获取微博列表
    	try {
    		HttpData recvData = HttpClient.listRequest(Constants.SERVER_ADDR, 
    				user, sign);
    		
    		if (recvData == null) {
    			Log.e(TAG, "recv data null.");
    			
    			return -1;
    		} else {
    			
    			/// 没有获取到数据
    			if (recvData.getByteArray() == null) {
    				return -1;
    			}
    			
    			/// 解析数据
    			String ResponseString = new String(recvData.getByteArray());
    			JSONObject object = null;
    			/// 解析错误码
				try {
					String strCode = null;
					
					object = new JSONObject(ResponseString);
					strCode = object.getString("code");
					code = Integer.parseInt(strCode);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

    			/// 状态码正确
    			if (code == 1) {
    				/// 解析签名
    				try {
    					JSONArray list = object.getJSONArray("dataList");
    					
    					for (int i=0; i<list.length(); i++) {
    						JSONObject ob = list.getJSONObject(i);
    						WeiboBean wb = new WeiboBean();
    						long weiboId = ob.getLong("weiboId");
    						wb.setWeiboId(weiboId);
    						wbList.add(wb);
    					}
    					
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
    			} else {
    				Log.e(TAG, "code = " + code);
    			}
    		}
		} catch (SocketTimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		exportListResponse(code, wbList);
		
		return 0;
	}
	
	/**
	 * 获取创意/魅力/贡献/童鞋的数目
	 * @param user 用户id/用户名
	 * @param sign 签名
	 * @param customer 待查用户id
	 * @return 请求返回状态
	 */
	public int count(String user, String sign, String customer) {
		long code = 0;
		String customerId = null;
		int creativeCount = 0;
		int charmCount = 0;
		int contributionsCount = 0;
		int colleaguesCount = 0;

		/// 获取创意/魅力/贡献/童鞋的数目
    	try {
    		HttpData recvData = HttpClient.countRequest(Constants.SERVER_ADDR, 
    				user, sign, customer);
    		
    		if (recvData == null) {
    			Log.e(TAG, "recv data null.");
    			
    			return -1;
    		} else {
    			
    			/// 没有获取到数据
    			if (recvData.getByteArray() == null) {
    				return -1;
    			}
    			
    			/// 解析数据
    			String ResponseString = new String(recvData.getByteArray());
    			JSONObject object = null;
    			/// 解析错误码
				try {
					String strCode = null;
					
					object = new JSONObject(ResponseString);
					strCode = object.getString("code");
					code = Integer.parseInt(strCode);
					//code = object.getLong("code");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    			
    			/// 状态码正确
    			if (code == 1) {
    				/// 解析签名
    				try {
    					JSONArray list = object.getJSONArray("dataList");

    					customerId = list.getString(0);
    					creativeCount = Integer.parseInt(list.getString(1));
    					charmCount = Integer.parseInt(list.getString(2));
    					contributionsCount = Integer.parseInt(list.getString(3));
    					colleaguesCount = Integer.parseInt(list.getString(4));
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
    			} else {
    				Log.e(TAG, "code = " + code);
    			}
    		}
		} catch (SocketTimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// / 导出获取创意/魅力/贡献/童鞋数目响应数据
		exportCountResponse(code, customerId, creativeCount, charmCount,
				contributionsCount, colleaguesCount);
		
		return (int) code;
	}
}
